package com.hxzy.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import com.hxzy.common.core.domain.model.LoginUser;
import com.hxzy.common.utils.SecurityUtils;
import com.hxzy.system.domain.SysOperLog;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * https://baomidou.com/pages/4c6bcf/
 * mybatis-plus对实体类自动填充功能
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.getOriginalObject() instanceof SysOperLog){
            return;
        }
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        LoginUser loginUserVO= SecurityUtils.getLoginUser();
        // 商家自己在注册
        if(Objects.isNull(loginUserVO)){
            this.strictInsertFill(metaObject, "createBy", String.class,"用户自己注册");
        }else {
            this.strictInsertFill(metaObject, "createBy", String.class, loginUserVO.getUsername());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.getOriginalObject() instanceof SysOperLog){
            return;
        }
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        LoginUser loginUserVO= SecurityUtils.getLoginUser();
        this.strictUpdateFill(metaObject, "updateBy", String.class,loginUserVO.getUsername());
    }
}
